package com.baiyang.app.service;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baiyang.app.tool.net.MyWebSocketSendUtil;

import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;

public class WebSocketService extends Service {
    public class MyWebSocketListener extends WebSocketListener {
        /**
         * Invoked when a web socket has been accepted by the remote peer and may begin transmitting
         * messages.
         */
        @Override
        public void onOpen(WebSocket webSocket, Response response) {
            Log.i("webSocket--->","连接打开");
        }

        private final String TYPE_RESULT = "result";
        private final String TYPE_MESSAGE = "message";

        /** Invoked when a text (type {@code 0x1}) message has been received.
         * 接受到来自服务器的消息
         * --再以广播的形式发送出去
         */
        @SuppressLint("LongLogTag")
        @Override
        public void onMessage(WebSocket webSocket, String text) {
            Intent intent = new Intent("com.baiyang.MESSAGE");
            JSONObject mes = JSONObject.parseObject(text);
            intent.putExtra("message", mes);
            sendOrderedBroadcast(intent,null);
            Log.i("webSocket get Message---->",text);
        }
        /** Invoked when the peer has indicated that no more incoming messages will be transmitted. */
        public void onClosing(WebSocket webSocket, int code, String reason) {
            Log.i("webSocket--->","连接关闭");
            MyWebSocketSendUtil.clear();
        }

        /**
         * Invoked when both peers have indicated that no more messages will be transmitted and the
         * connection has been successfully released. No further calls to this listener will be made.
         */
        public void onClosed(WebSocket webSocket, int code, String reason) {
            Log.i("webSocket--->","连接关闭");
            MyWebSocketSendUtil.clear();
        }

        /**
         * Invoked when a web socket has been closed due to an error reading from or writing to the
         * network. Both outgoing and incoming messages may have been lost. No further calls to this
         * listener will be made.
         */
        public void onFailure(WebSocket webSocket, Throwable t, Response response) {
            System.out.println(t);
            Log.i("webSocket--->","连接异常"+
                    t.getMessage());
            MyWebSocketSendUtil.clear();
        }
    }
    //Service被创建时回调
    @Override
    public void onCreate() {
        Log.i("service","oncreate");
        super.onCreate();
    }
    @Override
    public IBinder onBind(Intent intent) {
        return new serviceBinder();
    }
    public class serviceBinder extends Binder {
        private MyWebSocketListener myWebSocketListener = new MyWebSocketListener();
        public MyWebSocketListener getMyWebSocketListener() {
            return myWebSocketListener;
        }
    }

}
